package com.td.erhome.ui.sellhouse;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.td.erhome.R;
import com.td.erhome.adapter.MyPublishHouseListAdapter;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.HouseBean;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;
import com.td.erhome.utils.ConstUtils;
import com.td.erhome.utils.DialogUtils;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.UserManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MyPublishActivity extends BaseActivity {

    @Bind(R.id.img_back)
    ImageView mImgBack;
    @Bind(R.id.recyclview)
    XRecyclerView mRecyclerView;
    @Bind(R.id.tv_right)
    TextView mTvRight;
    private List<HouseBean.SellHouseListBean> mhouse_list = new ArrayList<>();
    private MyPublishHouseListAdapter mAdapter;
    private Subscription mSubscribe;
    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_publish;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new MyPublishHouseListAdapter(this, mhouse_list);
        mRecyclerView.setAdapter(mAdapter);
        getData();

        mSubscribe = RxBus.getInstance().toObserverable(Integer.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        if (integer == ConstUtils.DE_HOUSE) {
                            mAdapter.clearData();
                            page = 1;
                            getData();
                        }
                    }
                });

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mAdapter.clearData();
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });

    }

    @Override
    protected void releaseResource() {
        if (!mSubscribe.isUnsubscribed()) {
            mSubscribe.unsubscribe();
        }
    }


    public void getData() {

        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", UserManager.getIns().getUser().uid);
        params.put("page", page);
        Api.getRetrofit().getSellHouseList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<HouseBean>>(MyPublishActivity.this) {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mRecyclerView.refreshComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mRecyclerView.refreshComplete();
                    }

                    @Override
                    public void onNext(BaseBean<HouseBean> houseBeanBaseBean) {
                        super.onNext(houseBeanBaseBean);
                        mhouse_list = houseBeanBaseBean.getData().house_list;
                        mAdapter.addData(mhouse_list);
                        mRecyclerView.refreshComplete();
                    }

                });
    }

    @OnClick({R.id.img_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                DialogUtils.mesg(MyPublishActivity.this);
                break;
        }
    }
}
